import 'package:bluetooth_protocols_parser_plugin/library.dart';
import 'package:lx_widgets_library/lx_widgets_library.dart';
import 'package:safemap/safemap.dart';

// 固件信息响应模型
class FirmwareUpgradeRspModel extends BaseData {
  final List<FirmwareUpgradeInfoModel> firmwareUpgradeModels;
  final String summary;
  final String description;

  FirmwareUpgradeRspModel({
    required this.firmwareUpgradeModels,
    required this.summary,
    required this.description,
  });

  factory FirmwareUpgradeRspModel.fromJson(Map<String, dynamic> json) {
    SafeMap safeMap = SafeMap(json);
    return FirmwareUpgradeRspModel(
      firmwareUpgradeModels: (safeMap['version'].list ?? [])
          .map<FirmwareUpgradeInfoModel>(
              (item) => FirmwareUpgradeInfoModel.fromMap(item))
          .toList(),
      summary: safeMap['summary'].string ?? "",
      description: safeMap['description'].string ?? "",
    );
  }

  Map<String, dynamic> toMap() {
    return {
      'version': firmwareUpgradeModels.map((e) => e.toMap()).toList(),
      'summary': summary,
      'description': description,
    };
  }
}

class FirmwareUpgradeInfoModel {
  String filePath = "";
  String device = "";
  List<FirmwareUpgradeSubModel> versions = [];

  FirmwareUpgradeInfoModel.fromMap(Map<String, dynamic> json) {
    SafeMap safeMap = SafeMap(json);
    filePath = safeMap['file'].string ?? "";
    device = safeMap['device'].string ?? "";
    versions = (safeMap['versions'].list ?? [])
        .map<FirmwareUpgradeSubModel>(
            (item) => FirmwareUpgradeSubModel.fromJson(item)..file = filePath)
        .toList();
  }

  Map<String, dynamic> toMap() {
    return {
      'device': device,
      'versions': versions.map((e) => e.toMap()).toList(),
      'file': filePath,
    };
  }
}

class FirmwareUpgradeSubModel {
  final String device;
  final String latestVersion;
  String file = "";
  int get deviceId => int.tryParse(device) ?? 0;

  // 设备类型
  TargetDeviceType? get type => TargetDeviceType.getTypeByNum(0, 1);

  FirmwareUpgradeSubModel({
    required this.device,
    required this.latestVersion,
  });

  factory FirmwareUpgradeSubModel.fromJson(Map<String, dynamic> json) {
    SafeMap safeMap = SafeMap(json);
    return FirmwareUpgradeSubModel(
      device: safeMap['device'].string ?? "",
      latestVersion: safeMap['version'].string ?? "",
    );
  }

  Map<String, dynamic> toMap() {
    return {
      'device': device,
      'version': latestVersion,
    };
  }
}
